﻿using System;
using System.Web;
using System.Web.UI;
using Microsoft.Web.UI.WebControls;
using UDS.Components;

namespace UDS.Inc
{
    /// <summary>
    ///		ControlDepartmentTreeView 的摘要说明。
    /// </summary>
    [CLSCompliant(false)]
    public partial class ControlRoleTreeView : UserControl
    {
        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var rootNode = new TreeNode();
                rootNode.Text = "<span onmouseover=javascript:title='我的角色'>我的角色</span>";
                rootNode.ImageUrl = ResolveUrl("~/DataImages/person.gif");
                roleTreeView.Nodes.Add(rootNode);

                GetRoleNode(rootNode);
                roleTreeView.Nodes[0].Expanded = true;
            }

            base.OnLoad(e);
        }


        private void GetRoleNode(TreeNode parentNode)
        {
            roleTreeView.ShowToolTip = true;
            var db = new Database();

            using (var dr = db.RunDataReader("dbo.sproc_GetRole"))
            {
                while (dr.Read())
                {
                    var roleId = (int)dr["Role_ID"];

                    var node = new TreeNode();
                    node.ID = roleId.ToString();
                    node.Text = "<span onmouseover=javascript:title='" + HttpUtility.HtmlAttributeEncode(dr["Role_Description"].ToString()) + "'>" + HttpUtility.HtmlEncode(dr["Role_Name"].ToString()) + "</span>";
                    node.ImageUrl = ResolveUrl("~/DataImages/person.gif");
                    node.NavigateUrl = "ListView.aspx?Role_ID=" + roleId;
                    node.Target = "MainFrame";

                    parentNode.Nodes.Add(node);
                }
                dr.Close();
            }
        }
    }
}